/**
 * Created by Administrator on 2018/3/31.
 */
(function () {
    let $vue = null;
    let constantParams = JSON.parse(IOT.getSessionStore(URI.OPERATE.SINGLELAMP.CREATE_PALLET.PAGE));
    let action = constantParams.action;
    let map = null;
    let currentNodes=new Array();
    let _selectIMSI = function (ImsiList) {
        ImsiList = ImsiList || [];
        for (let i in ImsiList) {
            ImsiList[i].value = ImsiList[i].companyName;
        }
        // 选择所属单位
        $('#selectedIMSI').autocomplete({
            source: ImsiList,
            focus: function (event, ui) {
                $('#selectedIMSI').val(ui.item.label);
                return false;
            },
            select: function (event, ui) {
                $('#sselectedIMSI').val(ui.item.companyName);
                return false;
            }
        });
    };
    $vue = new Vue({
        el: '.add-groups-single-hook',
        data: {
            title: action === ACTION.CREATE ? '新增' : '修改',
            control_type:[{id: 1, name: '故障A'}, {id: 2, name: '故障B'},{id: 3, name: '故障C'}],
            info:{
                IMEI:"",
               lng:'',
                lat:''
            },
           ImsiList: [],
            /* detailInfo:{}*/
        },
        created: function () {
            if(action===ACTION.CREATE){

            }else if(action===ACTION.MODIFY){
               /* this.info = $.extend({}, this.info, storeParams);*/
            }
        },
        methods: {
            goBack: function () {
                M.Page.emitPrePage();
            },
            saveEdit:function () {
                $('.add-form-single-hook').validate({
                    debug: true,
                    rules: {
                        singleName:{
                            required:true,
                        }
                        /*  palletTypemNum:{
                         required:true,
                         },
                         palletTypeyNum:{
                         required:true,
                         },
                         palletTypeqNum:{
                         required:true,
                         }*/
                    },
                    submitHandler: function (form) {

                        //输入正整数
                        var regu = /^[1-9]\d*$/;
                        if(!regu.test($vue.$data.info.IMEI)){
                            layer.alert('IMEI必须为正整数');
                            return false;
                        }
                        IOT.showOverlay('保存中，请稍等...');
                        let params = $vue.$data.info;
                        IOT.getServerData(URI.OPERATE.SINGLELAMP.CREATE_PALLET.SAVE,params, function (ret) {
                            IOT.tips(ret.msg || '操作失败，请重试！', 'error', 1000, null);
                            IOT.hideOverlay();
                            if (ret && ret.code === 200) {
                                IOT.tips('保存成功！', 'success');
                                M.Table.refresh.all();
                                M.Page.emitPrePage();
                            } else {
                                IOT.tips(ret.msg || '操作失败，请重试！', 'error', 1000, null);
                            }
                        });
                        return false;
                    }
                });
            }
        },
        mounted: function () {
            this.$nextTick(() => {
                M.Request.getIMSIList({}, true, (ImsiList, ret) => {
                    _selectIMSI(ImsiList);
                    this.ImsiList = ImsiList;
                });
                initMap();
                if(action===ACTION.CREATE){
                    clickAddOverlay();
                }

                var mapData =[
                    {lat:26.2,lng:114.45,name:'灯泡A'},
                    {lat:33.2,lng:110.45,name:'灯泡Ad'},
                    {lat:29.1,lng:116.45,name:'灯泡Asb'},
                    {lat:31.2,lng:104.45,name:'灯泡Abc'},
                    {lat:29.2,lng:101.45,name:'灯泡Af'}
                ];
                drawOverlays(mapData);
            });
        }
    });
    //地图初始化
    var initMap = function() {
        map = new BMap.Map("add_single_map", {enableMapClick: false});    // 创建Map实例
        var point = new BMap.Point(103.73, 36.03);  // 创建点坐标
        map.centerAndZoom(point, 5); //初始化地图,设置中心点坐标和地图级别
        map.enableScrollWheelZoom(true);  //开启鼠标滚轮缩放*/
        map.addControl(new BMap.NavigationControl());
        map.addControl(new BMap.ScaleControl());
        map.addControl(new BMap.OverviewMapControl());
    };
    var setCity = function(){
        var cityCenter = function(result){
            var cityName = result.name;
            map.setCenter(cityName);
        };
        var myCity = new BMap.LocalCity();
        myCity.get(cityCenter);
    };
    //地图画标注点方法
    var drawOverlays = function(data) {

        map.clearOverlays();
        var pointArray = new Array();
        $.each(data, function (i, node) {
            var point = new BMap.Point(node.lng, node.lat);
            var fnIcon = new BMap.Icon("img/icon/icon1.png", new BMap.Size(32,51), {
                offset: new BMap.Size(0, 0),
            });
            var marker = new BMap.Marker(point, {icon: fnIcon});        // 创建标注
            map.addOverlay(marker);
            pointArray.push(point);
          /*  marker.addEventListener("click", function (e) {
            });*/
        });
        map.setViewport(pointArray);
    }
    //新增覆盖物
    var bOk =true;
    var clickAddOverlay =function () {

            map.addEventListener("click", function (e) {
                if(bOk == true) {
                    bOk =false;
                    var point = new BMap.Point(e.point.lng, e.point.lat);
                    $vue.$data.info.lng =e.point.lng;
                    $vue.$data.info.lat =e.point.lat;
                    var fnIcon = new BMap.Icon("img/icon/icon2.png", new BMap.Size(32,51), {
                        offset: new BMap.Size(0, 0),
                    });
                    var marker = new BMap.Marker(point, {icon: fnIcon});        // 创建标注
                    map.addOverlay(marker);
                    marker.enableDragging();
                    marker.addEventListener("dragend",attribute);
                    function attribute(){
                        var p = marker.getPosition();
                        $vue.$data.info.lng =p.lng;
                        $vue.$data.info.lat =p.lat;
                    }
                }else{
                    return
                }

            });
    }

})();